package com.Travit;

import java.io.IOException;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

public class DistributorFinal extends Activity{

	ImageButton back;
	ImageButton logout;
	Button save;
	TextView destinationadd;
	TextView sourceadd;
	EditText employee;
	EditText origdistributor;
	
	
	String [] nameOfAllDestination;
	
	private static String loggedid;
	private static String urlRequestValidateBoxBarcode = "http://project492.engr.sjsu.edu/travITvalidateProduct.php";
	private static String urlRequestupdateShipping = "http://project492.engr.sjsu.edu/travITupdateShipping.php";
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		Bundle extras = getIntent().getExtras();
		loggedid = extras.getString("loggedid");
		setContentView(R.layout.shipping);
		Log.v("should", "be in Dfinal");
		
		
		save = (Button) findViewById(R.id.shipButton);
		
		String boxBarCodeID = "1000";
		destinationadd = (TextView) findViewById(R.id.destinationAddress);
		sourceadd = (TextView) findViewById(R.id.sourceAddress);
		employee = (EditText) findViewById(R.id.shippedByEmployeeName);
		origdistributor = (EditText) findViewById(R.id.currentDistributorAddress);
		
		employee.setText("John Cena");
		origdistributor.setText("some street");
		
		HttpGet request = new HttpGet(urlRequestupdateShipping
				+ "?boxbarcodeID="+boxBarCodeID);
		HttpClient client = new DefaultHttpClient();
		try{
			HttpResponse response=client.execute(request);
			HttpEntity entity = response.getEntity();
			Log.v("entity:",entity.toString());
			String responseResult = EntityUtils.toString(entity);
			Log.v("name of all citires of all stores result::",responseResult);
			Log.v("response result",""+responseResult.charAt(2));
			Log.v("hahahahhahaha","hahahahhaha");
			String listOfDestinations=";"+responseResult;
			String[]nameOfAllDestination=listOfDestinations.split(";");
			for(int i=0;i<nameOfAllDestination.length;i++)
			{
				Log.v(""+i, nameOfAllDestination[i]);
			}
			sourceadd.setText(""+nameOfAllDestination[3]);
			destinationadd.setText(""+nameOfAllDestination[2]);
		
		}
		catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

//		logout = (ImageButton) findViewById(R.id.logout);
//
//		logout.setOnClickListener(new OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//				// TODO Auto-generated method stub
//				//SHOW INFORMATION FROM BARCODE
//				//initialize barcodeid
//				Intent newIntent = new Intent(v.getContext(), TravITActivity.class);
//				//newIntent.putExtra("venid", loggedid);
//		        startActivity(newIntent);
//			}			
//		});
//
		save.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.v("goingtoexecute", "try");
				String boxBarCodeID = "1000";
				String location="tokyoJapan";
				String prod="heineken";
				String updatedby="Cena";
				//Date dateupdated='2011-10-12';
				String sour="12345 abcde st, san jose, ca 95111";
				String dest="1234 abcd st, san jose, ca 95112";
				String stats="shipped";
//				EditText inputintermediate = (EditText) findViewById(R.id.destinationAddress);
//				String tempintermediate = inputintermediate.getText().toString();
//				Log.v("textentered", ""+tempintermediate);
				HttpClient client = new DefaultHttpClient();
				
//						+ "?intermediatePosition="+tempintermediate
//						+ "&boxbarcodeID="+boxBarCodeID);
				try {
					HttpPost request = new HttpPost(urlRequestupdateShipping
							+ "?boxbarcodeID=" + boxBarCodeID
							+ "&locationID=" + location
							+ "&prodname=" + prod
							+ "&updatedby=" + updatedby
							+ "&dateupdated=" + "2011-10-12"
							+ "&source=" + sour
							+ "&dest=" + dest
							+ "&statusID=" + stats);
					Log.v("trying", "before execution");
					List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
					nameValuePairs.add(new BasicNameValuePair("boxbarcodeID", boxBarCodeID));
					nameValuePairs.add(new BasicNameValuePair("locationID", location));
					nameValuePairs.add(new BasicNameValuePair("prodname", prod));
					nameValuePairs.add(new BasicNameValuePair("updatedby", updatedby));
					nameValuePairs.add(new BasicNameValuePair("dateupdated", "2011-10-12"));
					nameValuePairs.add(new BasicNameValuePair("source", sour));
					nameValuePairs.add(new BasicNameValuePair("dest", dest));
					nameValuePairs.add(new BasicNameValuePair("statusID", stats));
				    request.setEntity(new UrlEncodedFormEntity(nameValuePairs));

					HttpResponse response = client.execute(request);
					HttpEntity entity = response.getEntity();
					String responseResult = EntityUtils.toString(entity);
					Log.v("executing", "" + responseResult);
					if(responseResult.equalsIgnoreCase("Valid Box"))
					{
						Log.v("in if", "valid box");
						Intent newIntent = new Intent(v.getContext(), Distributor.class);
						newIntent.putExtra("venid", loggedid);
						startActivity(newIntent);
					}
					else
					{
						Log.v("in else", "valid box");
						Intent newIntent = new Intent(v.getContext(), Distributor.class);
						newIntent.putExtra("venid", loggedid);
						startActivity(newIntent);
					}
				} catch (ClientProtocolException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				}
				Intent newIntent = new Intent(v.getContext(), Distributor.class);
				newIntent.putExtra("venid", loggedid);
				startActivity(newIntent);
			}			
		});
//
//		back = (ImageButton) findViewById(R.id.backbutton);
//		back.setOnClickListener(new OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//				// TODO Auto-generated method stub
//				Intent newIntent = new Intent(v.getContext(), Distributor.class);
//				newIntent.putExtra("venid", loggedid);
//				startActivity(newIntent);
//			}			
//		});

	}

}
